package com.tarena.lbs.user.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.user.dao.mapper.UserMapper;
import com.tarena.lbs.user.dao.mapper.UserTagsMapper;
import com.tarena.lbs.user.dao.repository.UserRepository;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.query.UserQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserRepositoryImpl implements UserRepository {

    private static final Logger log = LoggerFactory.getLogger(UserRepositoryImpl.class);
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserTagsMapper userTagsMapper;

    @Override
    public UserPO getByPhone(String phone) {
        QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public Long countUserByPhone(String phone) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("phone",phone);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public void save(UserPO po) {
        userMapper.insert(po);
    }

    @Override
    public UserPO getById(Integer userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public PageInfo<UserPO> pagesList(UserQuery userQuery) {
            QueryWrapper<UserPO> queryWrapper = assembleUserQuery(userQuery);
            //PageHelper.startPage 之间不能有代码
            PageHelper.startPage(userQuery.getPageNo(), userQuery.getPageSize());
            List<UserPO> pos = userMapper.selectList(queryWrapper);
            //selectList 数据库操作 之间不能有代码
            return new PageInfo<>(pos);
        }



    private QueryWrapper<UserPO> assembleUserQuery(UserQuery userQuery) {
            QueryWrapper<UserPO> queryWrapper = new QueryWrapper<>();
            //1.accountPhone 账号手机号 模糊查询 where account_phone=#{phone}
            if (StringUtils.isNotBlank(userQuery.getPhone())){
                queryWrapper.like("phone",userQuery.getPhone());
            }
            //2.username 用户名 模糊查询 非空 where username like(%#{}%)
            if (StringUtils.isNotBlank(userQuery.getUserName())){
                queryWrapper.like("username",userQuery.getUserName());
            }
            //3.providerType 渠道来源 相等 非空 where provider_type = #{providerType}
            if (StringUtils.isNotBlank(userQuery.getProviderType())){
                queryWrapper.eq("provider_type",userQuery.getProviderType());
            }
            //4.startTime 开始时间 模糊查询 非空 where start_time >= #{startTime}
            if (userQuery.getStartTime() != null){
                queryWrapper.ge("reg_time",userQuery.getStartTime());
            }
            //5.endTime 结束时间 模糊查询 非空 where end_time <= #{endTime}
            if (userQuery.getEndTime() != null){
                queryWrapper.le("reg_time",userQuery.getEndTime());
            }
            return queryWrapper;
        }
}

